namespace ScrumSprintMonitor.ServerAdapter.TFS.TemplateAdaptors
{
    #region #using Directives

    using Infrastructure;

    using Isolation.TeamFoundationServer;

    #endregion

    public class ConchangoV3ScrumWorkItemAdapter : CoreWorkItemAdapter
    {
        public ConchangoV3ScrumWorkItemAdapter(IWorkItem workItem)
            : base(workItem)
        {
        }

        protected override void RetrieveState()
        {
            switch (WorkItem.State)
            {
                case "Deleted":
				case "Ignored":
					State = WorkItemState.Deleted;
					break;
				case "Descoped":
                case "Ready for Test":
                case "Done":
                case "Closed":
                    State = WorkItemState.Done;
                    break;
                case "Active":
                case "In Progress":
                    if (Type == WorkItemType.SprintBacklogItem)
                    {
                        RetrieveDoneWork();

                        if (DoneWork == 0.0)
                        {
                            State = WorkItemState.NotDone;
                            break;
                        }
                    }

                    State = WorkItemState.InProgress;
                    break;
                default:
                    State = WorkItemState.NotDone;
                    break;
            }
        }

        protected override void RetrieveDoneWork()
        {
            if (Type != WorkItemType.SprintBacklogItem)
            {
                DoneWork = double.NaN;
                return;
            }

            object estimatedEffort = WorkItem["Scrum.v3.EstimatedEffort"];
            DoneWork = (estimatedEffort != null ? (double) estimatedEffort - RemainingWork : 0.0);
        }

        protected override void RetrieveRemainingWork()
        {
            if (Type != WorkItemType.SprintBacklogItem)
            {
                RemainingWork = double.NaN;
                return;
            }

            if (WorkItem.State == "Deleted")
            {
                RemainingWork = 0.0;
            }
            else
            {
                object remainingWork = WorkItem["Scrum.v3.WorkRemaining"];

                RemainingWork = (remainingWork == null ? double.NaN : (double) remainingWork);
            }
        }

        protected override void RetrieveType()
        {
            switch (WorkItem.Type)
            {
                case "Sprint Backlog Task":
                    Type = WorkItemType.SprintBacklogItem;
                    break;
                case "Product Backlog Item":
                    Type = WorkItemType.UserStory;
                    break;
                case "Bug":
                    Type = WorkItemType.Defect;
                    break;
                default:
                    Type = WorkItemType.Unknown;
                    break;
            }
        }
    }
}